//
//  WBHomeViewController.swift
//  Weibo
//
//  Created by kobe on 2016/12/22.
//  Copyright © 2016年 kobe. All rights reserved.
//

import UIKit

//原创微博的可重用cellid
private let cellid = "cellid"

//被转发微博的cellid
private let retweetedCellId = "retweetedCellId"

class WBHomeViewController: WBBaseViewController {
    
    fileprivate lazy var listViewModel = WBStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        

        // Do any additional setup after loading the view.
    }
    
    override func loadData() {
        
        print("准备刷新")
        
        refreshController?.beginRefreshing()
        
        //模拟延时加载
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
           
            
            self.listViewModel.loadStatus(pullup: self.isPull) { (isSuccess,shouldRefresh) in
                
                
                //结束刷新控件
                self.refreshController?.endRefreshing()
                
                //恢复上拉刷新标记
                self.isPull = false
                
                //刷新表格
                if shouldRefresh {
                    self.tableView?.reloadData()
                }
                
                
            }
        })
        
        
        
        
        
        
    }
    
    func showFriends() {
        print("haha")
        let vc = WBDemoViewController()
        navigationController?.pushViewController(vc, animated: true)
    }


}


// MARK: - 表格数据源方法 具体的数据源方法实现不需要super
extension WBHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //取出视图模型 根据视图模型判断可重用cell
        let viewModel = listViewModel.statusList[indexPath.row]

        /// 选择使用哪个cellid
        let chooseCellid = (viewModel.status.retweeted_status != nil) ? retweetedCellId : cellid
        
        //FIXME:修改id
        let cell = tableView.dequeueReusableCell(withIdentifier: chooseCellid,for: indexPath) as! WBStatusTableViewCell
        

        cell.viewModel = viewModel
        
        return cell
    }
}

//设置界面
extension WBHomeViewController {
    //重写父类方法
    override func setupTableview() {
        super.setupTableview()
        
        //设置导航栏按钮
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 16, target: self, action: #selector(showFriends))
        
//        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellid)
        
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: cellid)
        tableView?.register(UINib(nibName: "WBStatusRetweetedlCell", bundle: nil), forCellReuseIdentifier: retweetedCellId)

        
        tableView?.separatorStyle = .none
        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 300
        
        setTitle()
    }
    
    fileprivate func setTitle() {
        
        let button = WBTitleButton(title: WBNetworkManager.shared.userAccount.screen_name)
        navItem.titleView = button
        
        button .addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
    }
    
    @objc func clickTitleButton(btn:UIButton) {
        //设置选中状态
        btn.isSelected = !btn.isSelected
        
        
    }
    
}
